package com.sheep.jucdemo.part6.part6_2;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Date 2022/4/24 22:06
 * @Created by sheep
 */
public class Test6_2_2_1 {

    private final static ReentrantLock lock = new ReentrantLock();
    private final static Condition condition = lock.newCondition();

    public static void main(String[] args) {
        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                try {
                    System.out.println("threadOne begin wait");
                    condition.await();
                    System.out.println("threadOne end wait");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        });


        Thread threadTwo = new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                try {
                    System.out.println("threadTwo begin signal");
                    condition.signal();
                    System.out.println("threadTwo end signal");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }
        });
        threadOne.start();
        threadTwo.start();

    }
}
